#include "widget.h"
#include "ui_widget.h"
#include <QTimer>
#include <QDebug>
#include <QRegExp>
#include <QRegExpValidator>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->pushButton->setEnabled(false);

    timer = new QTimer(this);

    connect(timer,&QTimer::timeout,this,&Widget::handle);
    timer->start(100);

    ui->lineEdit_name->setPlaceholderText("请输入姓名：");
    ui->lineEdit_name->setClearButtonEnabled(true);


    ui->lineEdit_password->setPlaceholderText("请输入密码：");
    ui->lineEdit_password->setClearButtonEnabled(true);
    ui->lineEdit_password->setEchoMode(QLineEdit::Password);

    QRegExp exp(R"(1\d{2}-\d{4}-\d{4})");
    ui->lineEdit_phone->setValidator(new QRegExpValidator(exp));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::handle()
{
    int value = ui->progressBar->value();
    if(value >= 100.0)
    {
        timer->stop();
        return;
    }
    qDebug() << value;
    ui->progressBar->setValue(value + 1);
}


void Widget::on_pushButton_clicked()
{
    QString str = ui->lineEdit_name->text() + " " + ui->lineEdit_password->text();
    ui->label->setText(str);
}



void Widget::on_lineEdit_phone_textEdited(const QString &arg1)
{
    qDebug() << "文本改变";
    QString text = arg1;
    int value;

    if(ui->lineEdit_phone->validator()->validate(text,value) == QValidator::Acceptable)
    {
        ui->pushButton->setEnabled(true);
    }
    else
    {
        ui->pushButton->setEnabled(false);
    }
}
